//
//  WBHomeTableViewCell.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class WBHomeTableViewCell: UITableViewCell {

    var bottomConstaint: Constraint?
    
    var homeViewModel: WBHomeViewModel?{
    
        didSet{
        
            orginalView.orginalViewModel = homeViewModel
        
            footerView.footerViewModel = homeViewModel
            
            
            //更新约束之前卸载之前的约束
            bottomConstaint?.uninstall()
            
            if homeViewModel?.status?.retweeted_status == nil {
                // 原创微博
                print("原创微博")
                
                retweetView.isHidden = true
                
                // 更新约束
                footerView.snp_updateConstraints(closure: { (make) in
                    
                    // 用属性去记录这个约束,以便下次重用的时候,卸载
                    bottomConstaint =  make.top.equalTo(orginalView.snp_bottom).constraint
                })
                
            } else {
                // 转发微博
                print("转发微博")
                
                retweetView.isHidden = false
                // 赋值
                retweetView.retweetViewModel = homeViewModel
                
                
                // 更新约束
                footerView.snp_updateConstraints(closure: { (make) in
                    bottomConstaint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }

            
        }
    
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
    private func setupUI(){
    
        backgroundColor = UIColor(white: 240 / 255, alpha: 1)
        contentView.addSubview(orginalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(footerView)
        
        
        orginalView .snp_makeConstraints { (make) in
            
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top).offset(5)
            make.right.equalTo(contentView.snp_right)
     
        }
        
        
        retweetView.snp_makeConstraints { (make) in
            
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(orginalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
            
        }
        
        
        footerView.snp_makeConstraints { (make) in
            
          bottomConstaint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(35)
            
            make.bottom.equalTo(contentView.snp_bottom)
            
        }
        
        
    
    }
    
    
    
    
    private lazy var orginalView : WBOrginalView = WBOrginalView()
    
    private lazy var footerView : WBFooterView = WBFooterView()
    
    private lazy var retweetView : WBRetweetView = WBRetweetView()
    
}
